﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace HoaDon
{
	public partial class LocHoaDon : System.Web.UI.Page
	{
		private System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo("vi-VN");
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				dttuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
				dtdenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
				drnhanvien.DataSource = Get_NhanVien();
				drnhanvien.DataValueField = "UserID";
				drnhanvien.DataTextField = "UserName";
				drnhanvien.DataBind();
				drnhanvien.SelectedValue = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();

				drcuahang.DataSource = Get_CuaHang();
				drcuahang.DataValueField = "KhoID";
				drcuahang.DataTextField = "KhoName";
				drcuahang.DataBind();
				drcuahang.SelectedValue = "All";

				drkhuvuc.DataSource = Get_KhuVuc();
				drkhuvuc.DataValueField = "nhvvId";
				drkhuvuc.DataTextField = "nhvvName";
				drkhuvuc.DataBind();
				drkhuvuc.SelectedValue = "All";

				if (drkhuvuc.SelectedValue != null)
				{
					drban.DataSource = Get_Ban(drkhuvuc.SelectedValue.ToString());
					drban.DataValueField = "vvID";
					drban.DataTextField = "vvName";
					drban.DataBind();
				}

				gdxdshoadon.DataSource = Get_Data();
				gdxdshoadon.DataBind();
				if (gdxdshoadon.Rows.Count > 0)
					gdxdshoadon.Rows[gdxdshoadon.Rows.Count - 1].Cells[8].Enabled = false;
			}
		}

		protected void btOk_Click(object sender, EventArgs objec)
		{
			gdxdshoadon.DataSource = Get_Data();
			gdxdshoadon.DataBind();
			if (gdxdshoadon.Rows.Count > 0)
				gdxdshoadon.Rows[gdxdshoadon.Rows.Count - 1].Cells[8].Enabled = false;
		}

		private DataTable Get_Data()
		{
			string sCommand = "";
			sCommand += " AND Ngay_ct >= '" + DateTime.Parse(dttuNgay.Text, cultureinfo).ToString("yyyyMMdd") + "'";
			sCommand += " AND Ngay_ct <= '" + DateTime.Parse(dtdenNgay.Text, cultureinfo).ToString("yyyyMMdd") + "'";

			if (drkhuvuc.SelectedValue != null)
			{
				if (drkhuvuc.SelectedValue.ToString() != "All")
					if (drban.SelectedValue.ToString() != "AllB")
					{
						sCommand += " AND FK_VvID = '" + drban.SelectedValue.ToString() + "'";
					}
					else
					{
						sCommand += " AND FK_VvID IN (SELECT vvID FROM dmvv WHERE dmvv.FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND FK_NhvvID = '" + drkhuvuc.SelectedValue.ToString() + "')";
					}
			}

			if (drnhanvien.SelectedValue != null)
			{
				if (drnhanvien.SelectedValue.ToString() != "All")
					sCommand += " AND FK_bpkdID = '" + drnhanvien.SelectedValue.ToString() + "'";
			}

			if (drcuahang.SelectedValue != null)
			{
				if (drcuahang.SelectedValue.ToString() != "All")
					sCommand += " AND FK_khoID = '" + drcuahang.SelectedValue.ToString() + "'";
			}

			if (ckAll.Checked == false)
				sCommand += " AND Status = '1'";

			DataTable dtResult = new DataTable();
			dtResult = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM vph81 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "'" + sCommand + " ORDER BY Ngay_ct");

			if (dtResult.Rows.Count > 0)
			{
				decimal t_tienhang = 0;
				decimal t_tienck = 0;
				decimal t_tien = 0;
				for (int i = 0; i < dtResult.Rows.Count; i++)
				{
					t_tienhang += Convert.ToDecimal(dtResult.Rows[i]["Tien_hang"].ToString());
					t_tienck += Convert.ToDecimal(dtResult.Rows[i]["Tien_ck"].ToString());
					t_tien += Convert.ToDecimal(dtResult.Rows[i]["T_Tien"].ToString());
				}
				DataRow dr = dtResult.NewRow();
				dr["Stt_rec"] = "TỔNG CỘNG";
				dr["Tien_hang"] = "" + t_tienhang;
				dr["Tien_ck"] = "" + t_tienck;
				dr["T_Tien"] = "" + t_tien;
				dtResult.Rows.Add(dr);
			}
			return dtResult;
		}

		protected void btIn_Click(object sender, EventArgs objec)
		{
			DataTable dtReport = new DataTable();
			dtReport = Get_Data();
			rptBCBKBH rpt = new rptBCBKBH { DataSource = dtReport };
			rpt.ma_tu_dong.DataBindings.Add("Text", dtReport, "STT_rec");
			rpt.ngay_ct.DataBindings.Add("Text", dtReport, "Ngay_ct", "{0:dd/MM/yyyy}");
			rpt.ban.DataBindings.Add("Text", dtReport, "vvName");
			rpt.tong_tien.DataBindings.Add("Text", dtReport, "T_tien", "{0:#,#}");
			rpt.nguoi_tao.DataBindings.Add("Text", dtReport, "user_id0");
			rpt.ngay_tao.DataBindings.Add("Text", dtReport, "datetime0", "{0:dd/MM/yyyy}");
			rpt.nguoi_sua.DataBindings.Add("Text", dtReport, "user_id2");
			rpt.ngay_sua.DataBindings.Add("Text", dtReport, "datetime2", "{0:dd/MM/yyyy}");

			rpt.Parameters["tungay"].Value = dttuNgay.Text;
			rpt.Parameters["denngay"].Value = dtdenNgay.Text;
			rpt.Parameters["khuvuc"].Value = drkhuvuc.SelectedItem.ToString();
			rpt.Parameters["tenban"].Value = drban.SelectedItem.ToString();
			Session["Report"] = rpt;
			Response.Redirect("~\\Print.aspx");
		}

		protected void btXoa_Click(object sender, EventArgs ap)
		{
			ImageButton bt = (ImageButton)sender;
			SqlEXEC.ExecCommand(Session["strConn"].ToString(), "EXEC [fs_DeleteHDA] '" + bt.CommandArgument + "','" + Session["FK_DatabaseID"].ToString() + "', '1'");

			gdxdshoadon.DataSource = Get_Data();
			gdxdshoadon.DataBind();
			if (gdxdshoadon.Rows.Count > 0)
				gdxdshoadon.Rows[gdxdshoadon.Rows.Count - 1].Cells[8].Enabled = false;
		}

		protected void btXem_Click(object sender, EventArgs ap)
		{
			ImageButton bt = (ImageButton)sender;
			string hoadon_tien = bt.CommandArgument;
			string[] s = hoadon_tien.Split('_');
			Session["vHDA_Stt_Rec"] = s[0];
			Response.Redirect("~\\DSMon.aspx");
		}

		private DataTable Get_NhanVien()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(WebConfigurationManager.ConnectionStrings["proxyconn"].ConnectionString, "SELECT UserID, UserName FROM Userinfo WHERE FK_DatabaseID = (SELECT FK_DatabaseID FROM Userinfo a WHERE a.UserID = '" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "')");
			DataRow newR = dt.NewRow();
			newR["UserID"] = "All";
			newR["UserName"] = "Tất cả";
			dt.Rows.Add(newR);
			return dt;
		}

		private DataTable Get_CuaHang()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmkho WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND Status = '1'");
			DataRow newR = dt.NewRow();
			newR["KhoID"] = "All";
			newR["KhoName"] = "Tất cả";
			dt.Rows.Add(newR);
			return dt;
		}

		private DataTable Get_KhuVuc()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmnhvv WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND Status = '1'");
			DataRow newR = dt.NewRow();
			newR["nhvvId"] = "All";
			newR["nhvvName"] = "Tất cả";
			dt.Rows.Add(newR);
			return dt;
		}

		protected void drkhuvuc_SelectedChanged(object sender, EventArgs objec)
		{
			if (drkhuvuc.SelectedValue != null)
			{
				drban.DataSource = Get_Ban(drkhuvuc.SelectedValue.ToString());
				drban.DataValueField = "vvID";
				drban.DataTextField = "vvName";
				drban.DataBind();
				drban.SelectedValue = "AllB";
			}
		}

		private DataTable Get_Ban(string nhomban)
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmvv WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND FK_NhvvID = '" + nhomban + "' AND Status = '1'");
			if (dt.Columns.Count == 0)
			{
				dt.Columns.Add("vvID", typeof(string));
				dt.Columns.Add("vvName", typeof(string));
			}
			DataRow newR = dt.NewRow();
			newR["vvID"] = "AllB";
			newR["vvName"] = "Tất cả";
			dt.Rows.Add(newR);

			return dt;
		}
	}
}
